<?php
/**
 * Created by PhpStorm.
 * User: bxm
 * Date: 2022/7/4
 * Time: 23:40
 */

namespace app\api\controller\worker;
use app\api\controller\Base;
use app\common\model\AdminModel;
use app\common\model\node\NodeModel;
use app\common\model\node\NodeProcessModel;
use app\common\model\offer\GoodsModel;
use app\common\model\worker\CustomerAuthModel;
use app\common\model\worker\CustomerBudgetModel;
use app\common\model\worker\CustomerGoodsDesertModel;
use app\common\model\worker\CustomerModel;
use app\common\model\worker\CustomerRulerDetailModel;
use app\common\model\worker\CustomerRulerModel;
use app\common\model\worker\TaskModel;
use think\facade\Db;
use think\Request;
use think\Validate;

class CustomerGoodsDesertController extends Base
{
    private $model;
    private $taskModel;
    /**
     * 构造方法
     * @access public
     * @throws \Exception
     */
    public function __construct()
    {
        parent::__construct();
        $this->model = new CustomerGoodsDesertModel();
        $this->taskModel = new TaskModel();
        $this->checkLogin();

    }
    //放弃品类
    public function discard(){
        $roles_ids = $this->role_ids;
        if(!in_array(7,$roles_ids)){

            $this->result([],'该用户没有权限，只有业务员有操作权限！',0);
        }
        $data = $this->request->param();

        $rule = [
            'customer_id|客户id'=>'require',
            'customer_brand|客户选择品牌'=>'require',
            'desert_status|放弃原因'=>'require|number',
            'goods_name|商品名称'=>'require',
            'goods_data_id|客户商品商品'=>'require',

        ];
        //放弃原因 1价格因素 2产品满足不了客户要求 3服务不满意 4 贷款周期满足不了客户 5推荐人不满意 6其他
        $desert_status = $data['desert_status'];
        if($desert_status == 1){
            $rule['retail_price|零售价格'] = 'require';
            $rule['discount_price|优惠金额'] = 'require';
            $rule['final_price|打折后金额'] = 'require';
            $rule['budget_price|客户预算金额'] = 'require';
        }

        $this->validate($data,$rule);
        $data['action_admin_id'] = $this->loginInfo['id'];
        $customerId = $this->request->param('customer_id');
        //todo 验证是否有更改权限
        //if($info['customer_status']==2) exception('客户已经成单，不允许执行操作');
        // 启动事务
        Db::startTrans();
        $info = CustomerGoodsDesertModel::create($data);
        if(!$info){
            // 回滚事务
            Db::rollback();
            $this->result([], "操作失败", 0);
        }
        $nodeProcess = NodeProcessModel::where('node_id', 10)->order('id', 'asc')->find();
        $dataTask = [
            'customer_id' => $data['customer_id'],
            'order_id' => 0,
            'node_id' => 10,
            'process_id' => $nodeProcess['id'],
            'receive_admin_id' => $nodeProcess['audit_admin_id'],
            'action_table_id'=>$info['id'],//操作表id
            'admin_id'=>$this->loginInfo['id']
        ];
        $result = $this->taskModel->commonAddTask($dataTask);
        if(!$result){
            // 回滚事务
            Db::rollback();
            $this->result([], "操作失败", 0);
        }
        Db::commit();
        $this->taskModel->sendTaskInfo($dataTask['receive_admin_id'],$nodeProcess['process_name']);

        $this->result([], "操作成功", 1);
    }
}